package com.CampusLife.controller;


import com.CampusLife.bean.Admin;
import  com.CampusLife.bean.User;
import com.CampusLife.until.DeleteImages;
import com.CampusLife.until.ImagesUpload;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import  com.CampusLife.service.IUerService;
import  com.CampusLife.until.Encrypt;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 赵海军
 * @createTime 2020-01-16 17:49
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUerService uerService;

    /**
     *
       查询所有用户
     */

    @RequestMapping("/query")
    public String query(Model model){
      List<User> list=uerService.queryAll();
      model.addAttribute("list",list);
      return "admin/userbasic";
    }
    /**
     * 进入用户添加页面
     */
    @RequestMapping("into-add")
    public String intoadd(){
        return "admin/adduser";
    }
    /**
     * 管理员添加用户
     */
      @RequestMapping("addUser")
      @ResponseBody
      public int addUser(String username){
       int count=   uerService.selectByUserName(username);
       if( count==0){
          String password="75eecd25c2b9c5b97cadb8d623b1f3ce";
          User user=new User();
          user.setUsername(username);
          user.setPassword(password);
          uerService.addUserName(user);
          return 0;
         // return "redirect:/user/query";
           }
       else if (count==1){
           return 1;
       }else {
           return -500;
       }
    }
    /**
     * 重置密码
     */
    @RequestMapping("/updatePasswordAdmin")
    public String updatePasswordAdmin(int id){
        User user=new  User();
        user.setId(id);
        user.setPassword("75eecd25c2b9c5b97cadb8d623b1f3ce");
        uerService.updatePasswordAdmin(user);
        return "redirect:/user/query";
    }

        //跳转到修改页面
        @RequestMapping("/jumpUpdateUserEdit")
    public String jumpUpdateUserEdit(Integer id,Model model){
        User user = uerService.queryById(id);
        model.addAttribute("user",user);
        return "home/updateUser";
    }
    //修改·用户·    /图片上传
    @RequestMapping(path = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    public int updateUser(@RequestParam("file")MultipartFile file, HttpServletRequest request,
                         HttpServletResponse response, String phone,String address,String nickname,HttpSession httpSession ) throws Exception {
        if (!file.isEmpty()){
            ImagesUpload imagesUpload =new ImagesUpload();
            String photo=  imagesUpload.Upload(file);
            User user=new User();
            Integer userId=(int)httpSession.getAttribute("id");
            user.setId(userId);
            user.setAddress(address);
            user.setPhone(phone);
            user.setNickname(nickname);
            user.setPhoto(photo);
            uerService.update(user);
            return  0;}
        //return "redirect:/user/info";}

    else if (file.isEmpty()) {
        return -500;
    }
        else {
        return 1;
    }
    }
    /**
     * 根据Id删除用户
     */

  @RequestMapping("/deleteUser")
    public String deleteUser( Integer id) throws  Exception{
       User user=uerService.queryById(id);
       if(user.getPhoto()!=null){
      DeleteImages deleteImages=new DeleteImages();
      File url= new File(user.getPhoto());
      deleteImages.deleteDirectory(url);}
        uerService.delete(id);
        return "redirect:/user/query";
  }
    /**
     * 根据学号模糊查询
     */
    @RequestMapping("/ByUsername")
    public String ByUsername( String username,Model model){
        List<User> list=uerService.ByUsername(username);
        model.addAttribute("list",list);
        return "admin/userbasic";

}

    /**
     * 根据用户昵称模糊查询
     * @param nickname
     * @param model
     * @return
     */
    @RequestMapping("/ByNikeName")
    public String ByNikeName( String nickname,Model model){
        List<User> list=uerService.ByNikeName(nickname);
        model.addAttribute("list",list);
        return "admin/userbasic";

    }
    @RequestMapping("/info")
    public  String userInfo(Model model, HttpSession httpSession){
        int i=(int)httpSession.getAttribute("id");
      User user=  uerService.queryById(i);
       model.addAttribute("user",user);
       System.out.println(user);
        return "home/userinfo";
    }

    @RequestMapping("/SMS")
    public String SMS(){
        return "home/Pwd1";
    }
    @RequestMapping("/getPhone")

    public  String  getPhone(String username,Model model,HttpSession httpSession)  {
       try {
           User user=  uerService.getPhone(username);
           httpSession.setAttribute("id_password", user.getId());
           String phone=user.getPhone();
           model.addAttribute("phone",phone);
           String phoneNumber = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
           model.addAttribute("phoneNumber",phoneNumber);

          return "home/Pwd2";
       }
       catch ( Exception e){
           return "home/erro";
       }

    }
    @RequestMapping("UpdatePassword")
    @ResponseBody
    public  int  update(Integer id,String password1,String password2){
      if (password1.equals(password2)) {
          User user = new User();
          user.setId(id);
          String ps = Encrypt.MD5(password1) + "wun";
          //再次加密防止密码外泄
          String ps1 = Encrypt.MD5(ps);
          user.setPassword(ps1);
          uerService.UpdatePassword(user);
          return 0;
         // return "home/tiaozhuan";
      }else {
          return 1;
         // return "home/Pwd4";
      }
    }

    @RequestMapping("/alter_ps")
    @ResponseBody
    public  int alterps(String password, String password1,String password2, HttpSession httpSession)throws Exception {
        String n = Encrypt.MD5(password) + "wun";
        String ps = Encrypt.MD5(n);
        int id=(int) httpSession.getAttribute("id");
        User user = uerService.queryById(id);
        if (user.getPassword().equals(ps)&&password1.equals(password2)) {
          User usr=new User();
            usr.setId(id);
            String m = Encrypt.MD5(password1) + "wun";
            String ps1 = Encrypt.MD5(m);
            usr.setPassword(ps1);
            uerService.UpdatePassword(usr);
            return 0;
        }else  if(!user.getPassword().equals(ps)){
            return 1;
        }
        else {
            return -500;
        }

    }
    @RequestMapping("/query1")
    public String query1(Model model){
        List<User> list=uerService.queryAll();
        model.addAttribute("list",list);
        return "admin/userbasic";
    }
}





